home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / svgabg31.zip / VGADEMO.PAS < prev    next >
Pascal/Delphi Source File  |  1991-12-06  |  47KB  |  1,693 lines

  1. program BGIDemo;
  2. {
  3.  
  4.   Turbo Pascal Borland Graphics Interface (BGI) demonstration
  5.   program. This program shows how to use many features of
  6.   the Graph unit.
  7.  
  8.   Copyright (c) 1985-89 by Borland International, Inc.
  9.  
  10. }
  11.  
  12. uses
  13.   Crt, Dos, Graph;
  14.  
  15.  
  16. const
  17.   { The five fonts available }
  18.   Fonts : array[0..4] of string[13] =
  19.   ('DefaultFont', 'TriplexFont', 'SmallFont', 'SansSerifFont', 'GothicFont');
  20.  
  21.   { The five predefined line styles supported }
  22.   LineStyles : array[0..4] of string[9] =
  23.   ('SolidLn', 'DottedLn', 'CenterLn', 'DashedLn', 'UserBitLn');
  24.  
  25.   { The twelve predefined fill styles supported }
  26.   FillStyles : array[0..11] of string[14] =
  27.   ('EmptyFill', 'SolidFill', 'LineFill', 'LtSlashFill', 'SlashFill',
  28.    'BkSlashFill', 'LtBkSlashFill', 'HatchFill', 'XHatchFill',
  29.    'InterleaveFill', 'WideDotFill', 'CloseDotFill');
  30.  
  31.   { The two text directions available }
  32.   TextDirect : array[0..1] of string[8] = ('HorizDir', 'VertDir');
  33.  
  34.   { The Horizontal text justifications available }
  35.   HorizJust  : array[0..2] of string[10] = ('LeftText', 'CenterText', 'RightText');
  36.  
  37.   { The vertical text justifications available }
  38.   VertJust   : array[0..2] of string[10] = ('BottomText', 'CenterText', 'TopText');
  39.  
  40. var
  41.   GraphDriver : integer;  { The Graphics device driver }
  42.   GraphMode   : integer;  { The Graphics mode value }
  43.   MaxX, MaxY  : word;     { The maximum resolution of the screen }
  44.   ErrorCode   : integer;  { Reports any graphics errors }
  45.   MaxColor    : word;     { The maximum color value available }
  46.   OldExitProc : Pointer;  { Saves exit procedure address }
  47.  
  48. function RealDrawColor(Color : Word) : Word;
  49. begin
  50.   if (GetMaxColor > 256) then
  51.     SetRgbPalette(1024,(Color SHR 10) AND 31,(Color SHR 5)AND 31,Color AND 31);
  52.   RealDrawColor := Color;
  53. end;
  54.  
  55. function RealFillColor(Color : Word) : Word;
  56. begin
  57.   if (GetMaxColor > 256) then
  58.     SetRgbPalette(1025,(Color SHR 10) AND 31,(Color SHR 5)AND 31,Color AND 31);
  59.   RealFillColor := Color;
  60. end;
  61.  
  62. function RealColor(Color : Word) : Word;
  63. begin
  64.   if (GetMaxColor > 256) then
  65.     SetRgbPalette(1026,(Color SHR 10) AND 31,(Color SHR 5)AND 31,Color AND 31);
  66.   RealColor := Color;
  67. end;
  68.  
  69. function WhitePixel : Word;
  70. var Clr : Word;
  71. begin
  72.   if (GetMaxColor > 256) then
  73.     Clr := 32767
  74.   else
  75.     Clr := 15;
  76.   WhitePixel := Clr;
  77. end;
  78.  
  79. function BluePixel : Word;
  80. var Clr : Word;
  81. begin
  82.   if (GetMaxColor > 256) then
  83.     Clr := 31
  84.   else
  85.     Clr := 1;
  86.   BluePixel := Clr;
  87. end;
  88.  
  89. function GreenPixel : Word;
  90. var Clr : Word;
  91. begin
  92.   if (GetMaxColor > 256) then
  93.     Clr := 31 SHL 5
  94.   else
  95.     Clr := 2;
  96.   GreenPixel := Clr;
  97. end;
  98.  
  99.  
  100. {$F+}
  101. procedure MyExitProc;
  102. begin
  103.   ExitProc := OldExitProc; { Restore exit procedure address }
  104.   CloseGraph;              { Shut down the graphics system }
  105. end; { MyExitProc }
  106. {$F-}
  107.  
  108. {$F+}
  109. function DetectVGA256 : integer;
  110. { Detects VGA or MCGA video cards }
  111. var
  112.   DetectedDriver : integer;
  113.   SuggestedMode  : integer;
  114. begin
  115.   DetectGraph(DetectedDriver, SuggestedMode);
  116.   if (DetectedDriver = VGA) or (DetectedDriver = MCGA) then
  117.   begin
  118.     Writeln('Which video mode would you like to use?');
  119.     Writeln('  0) 320x200x256');
  120.     Writeln('  1) 640x400x256');
  121.     Writeln('  2) 640x480x256');
  122.     Writeln('  3) 800x600x256');
  123.     Writeln('  4) 1024x768x256');
  124.     Write('> ');
  125.     Readln(SuggestedMode);
  126.     DetectVGA256 := SuggestedMode;
  127.   end
  128.   else
  129.     DetectVGA256 := grError; { Couldn't detect hardware }
  130. end; { DetectVGA256 }
  131. {$F-}
  132.  
  133. {$F+}
  134. function DetectVGA32k : integer;
  135. { Detects VGA or MCGA video cards }
  136. var
  137.   DetectedDriver : integer;
  138.   SuggestedMode  : integer;
  139. begin
  140.   DetectGraph(DetectedDriver, SuggestedMode);
  141.   if (DetectedDriver = VGA) or (DetectedDriver = MCGA) then
  142.   begin
  143.     Writeln('Which video mode would you like to use?');
  144.     Writeln('  0) 320x200x32k');
  145.     Writeln('  1) 640x350x32k');
  146.     Writeln('  2) 640x400x32k');
  147.     Writeln('  3) 640x480x32k');
  148.     Writeln('  4) 800x600x32k');
  149.     Write('> ');
  150.     Readln(SuggestedMode);
  151.     DetectVGA32k := SuggestedMode;
  152.   end
  153.   else
  154.     DetectVGA32k := grError; { Couldn't detect hardware }
  155. end; { DetectVGA32k }
  156. {$F-}
  157.  
  158. {$F+}
  159. function DetectTwk256 : integer;
  160. { Detects VGA or MCGA video cards }
  161. var
  162.   DetectedDriver : integer;
  163.   SuggestedMode  : integer;
  164. begin
  165.   DetectGraph(DetectedDriver, SuggestedMode);
  166.   if (DetectedDriver = VGA) or (DetectedDriver = MCGA) then
  167.   begin
  168.     Writeln('Which video mode would you like to use?');
  169.     Writeln('  0) 320x400x256');
  170.     Writeln('  1) 320x480x256');
  171.     Writeln('  2) 360x480x256');
  172.     Writeln('  3) 376x564x256');
  173.     Writeln('  4) 400x564x256');
  174.     Writeln('  5) 400x600x256');
  175.     Write('> ');
  176.     Readln(SuggestedMode);
  177.     DetectTwk256 := SuggestedMode;
  178.   end
  179.   else
  180.     DetectTwk256 := grError; { Couldn't detect hardware }
  181. end; { DetectVGA256 }
  182. {$F-}
  183.  
  184. {$F+}
  185. function DetectVGA16 : integer;
  186. { Detects VGA or MCGA video cards }
  187. var
  188.   DetectedDriver : integer;
  189.   SuggestedMode  : integer;
  190. begin
  191.   DetectGraph(DetectedDriver, SuggestedMode);
  192.   if (DetectedDriver = EGA) or (DetectedDriver = VGA) then
  193.   begin
  194.     Writeln('Which video mode would you like to use?');
  195.     Writeln('  0) 320x200x16');
  196.     Writeln('  1) 640x200x16');
  197.     Writeln('  2) 640x350x16');
  198.     Writeln('  3) 640x480x16');
  199.     Writeln('  4) 800x600x16');
  200.     Writeln('  5) 1024x768x16');
  201.     Write('> ');
  202.     Readln(SuggestedMode);
  203.     DetectVGA16 := SuggestedMode;
  204.   end
  205.   else
  206.     DetectVGA16 := grError; { Couldn't detect hardware }
  207. end; { DetectVGA256 }
  208. {$F-}
  209.  
  210. {$F+}
  211. function DetectTwk16 : integer;
  212. { Detects VGA or MCGA video cards }
  213. var
  214.   DetectedDriver : integer;
  215.   SuggestedMode  : integer;
  216. begin
  217.   DetectGraph(DetectedDriver, SuggestedMode);
  218.   if (DetectedDriver = VGA) then
  219.   begin
  220.     Writeln('Which video mode would you like to use?');
  221.     Writeln('  0) 704x528x16');
  222.     Writeln('  1) 720x540x16');
  223.     Writeln('  2) 736x552x16');
  224.     Writeln('  3) 752x564x16');
  225.     Writeln('  4) 768x576x16');
  226.     Writeln('  5) 784x588x16');
  227.     Writeln('  6) 800x600x16');
  228.     Write('> ');
  229.     Readln(SuggestedMode);
  230.     DetectTwk16 := SuggestedMode;
  231.   end
  232.   else
  233.     DetectTwk16 := grError; { Couldn't detect hardware }
  234. end; { DetectVGA256 }
  235. {$F-}
  236.  
  237. var
  238.   AutoDetectPointer : pointer;
  239.  
  240. procedure Initialize;
  241. { Initialize graphics and report any errors that may occur }
  242. var
  243.   InGraphicsMode : boolean; { Flags initialization of graphics mode }
  244.   PathToDriver   : string;  { Stores the DOS path to *.BGI & *.CHR }
  245.   UseWhichDriver : integer;
  246. begin
  247.   { when using Crt and graphics, turn off Crt's memory-mapped writes }
  248.   DirectVideo := False;
  249.   OldExitProc := ExitProc;                { save previous exit proc }
  250.   ExitProc := @MyExitProc;                { insert our exit proc in chain }
  251.   PathToDriver := '';
  252.   repeat
  253.     Writeln('Which driver to use?');
  254.     Writeln('  0) Svga256');
  255.     Writeln('  1) Svga16');
  256.     Writeln('  2) Svga32k');
  257.     Writeln('  3) Tweak256');
  258.     Writeln('  4) Tweak16');
  259.     Write('>');
  260.     Readln(UseWhichDriver);
  261.     if (UseWhichDriver = 0) then
  262.     begin
  263.       AutoDetectPointer := @DetectVGA256;
  264.       GraphDriver := InstallUserDriver('Svga256',AutoDetectPointer);
  265.     end
  266.     else if (UseWhichDriver=1) then
  267.     begin
  268.       AutoDetectPointer := @DetectVGA16;   { Point to detection routine }
  269.       GraphDriver := InstallUserDriver('SVGA16', AutoDetectPointer);
  270.     end
  271.     else if (UseWhichDriver=2) then
  272.     begin
  273.       AutoDetectPointer := @DetectVGA32k;
  274.       GraphDriver := InstallUserDriver('Svga32k',AutoDetectPointer);
  275.     end
  276.  
  277.     else if (UseWhichDriver=3) then
  278.     begin
  279.       AutoDetectPointer := @DetectTwk256;
  280.       GraphDriver := InstallUserDriver('Twk256',AutoDetectPointer);
  281.     end
  282.     else if (UseWhichDriver=4) then
  283.     begin
  284.       AutoDetectPointer := @DetectTwk16;
  285.       GraphDriver := InstallUserDriver('Twk16',AutoDetectPointer);
  286.     end;
  287.     GraphDriver := Detect;
  288.     InitGraph(GraphDriver, GraphMode, PathToDriver);
  289.     ErrorCode := GraphResult;             { preserve error return }
  290.     if ErrorCode <> grOK then             { error? }
  291.     begin
  292.       Writeln('Graphics error: ', GraphErrorMsg(ErrorCode));
  293.       if ErrorCode = grFileNotFound then  { Can't find driver file }
  294.       begin
  295.         Writeln('Enter full path to BGI driver or type <Ctrl-Break> to quit:');
  296.         Readln(PathToDriver);
  297.         Writeln;
  298.       end
  299.       else
  300.         Halt(1);                          { Some other error: terminate }
  301.     end;
  302.   until ErrorCode = grOK;
  303.   Randomize;                { init random number generator }
  304.   MaxColor := GetMaxColor;  { Get the maximum allowable drawing color }
  305.   MaxX := GetMaxX;          { Get screen resolution values }
  306.   MaxY := GetMaxY;
  307. end; { Initialize }
  308.  
  309. function Int2Str(L : LongInt) : string;
  310. { Converts an integer to a string for use with OutText, OutTextXY }
  311. var
  312.   S : string;
  313. begin
  314.   Str(L, S);
  315.   Int2Str := S;
  316. end; { Int2Str }
  317.  
  318. function RandColor : word;
  319. { Returns a Random non-zero color value that is within the legal
  320.   color range for the selected device driver and graphics mode.
  321.   MaxColor is set to GetMaxColor by Initialize }
  322. begin
  323.   RandColor := Random(MaxColor)+1;
  324. end; { RandColor }
  325.  
  326. procedure DefaultColors;
  327. { Select the maximum color in the Palette for the drawing color }
  328. begin
  329.   SetColor(RealDrawColor(WhitePixel));
  330. end; { DefaultColors }
  331.  
  332. procedure DrawBorder;
  333. { Draw a border around the current view port }
  334. var
  335.   ViewPort : ViewPortType;
  336. begin
  337.   DefaultColors;
  338.   SetLineStyle(SolidLn, 0, NormWidth);
  339.   GetViewSettings(ViewPort);
  340.   with ViewPort do
  341.     Rectangle(0, 0, x2-x1, y2-y1);
  342. end; { DrawBorder }
  343.  
  344. procedure FullPort;
  345. { Set the view port to the entire screen }
  346. begin
  347.   SetViewPort(0, 0, MaxX, MaxY, ClipOn);
  348. end; { FullPort }
  349.  
  350. procedure MainWindow(Header : string);
  351. { Make a default window and view port for demos }
  352. begin
  353.   DefaultColors;                           { Reset the colors }
  354.   ClearDevice;                             { Clear the screen }
  355.   SetTextStyle(DefaultFont, HorizDir, 1);  { Default text font }
  356.   SetTextJustify(CenterText, TopText);     { Left justify text }
  357.   FullPort;                                { Full screen view port }
  358.   OutTextXY(MaxX div 2, 2, Header);        { Draw the header }
  359.   { Draw main window }
  360.   SetViewPort(0, TextHeight('M')+4, MaxX, MaxY-(TextHeight('M')+4), ClipOn);
  361.   DrawBorder;                              { Put a border around it }
  362.   { Move the edges in 1 pixel on all sides so border isn't in the view port }
  363.   SetViewPort(1, TextHeight('M')+5, MaxX-1, MaxY-(TextHeight('M')+5), ClipOn);
  364. end; { MainWindow }
  365.  
  366. procedure StatusLine(Msg : string);
  367. { Display a status line at the bottom of the screen }
  368. begin
  369.   FullPort;
  370.   DefaultColors;
  371.   SetTextStyle(DefaultFont, HorizDir, 1);
  372.   SetTextJustify(CenterText, TopText);
  373.   SetLineStyle(SolidLn, 0, NormWidth);
  374.   SetFillStyle(EmptyFill, RealFillColor(0));
  375.   Bar(0, MaxY-(TextHeight('M')+4), MaxX, MaxY);      { Erase old status line }
  376.   Rectangle(0, MaxY-(TextHeight('M')+4), MaxX, MaxY);
  377.   OutTextXY(MaxX div 2, MaxY-(TextHeight('M')+2), Msg);
  378.   { Go back to the main window }
  379.   SetViewPort(1, TextHeight('M')+5, MaxX-1, MaxY-(TextHeight('M')+5), ClipOn);
  380. end; { StatusLine }
  381.  
  382. procedure WaitToGo;
  383. { Wait for the user to abort the program or continue }
  384. const
  385.   Esc = #27;
  386. var
  387.   Ch : char;
  388. begin
  389.   StatusLine('Esc aborts or press a key...');
  390.   repeat until KeyPressed;
  391.   Ch := ReadKey;
  392.   if Ch = Esc then
  393.     Halt(0)                           { terminate program }
  394.   else
  395.     ClearDevice;                      { clear screen, go on with demo }
  396. end; { WaitToGo }
  397.  
  398. procedure GetDriverAndMode(var DriveStr, ModeStr : string);
  399. { Return strings describing the current device driver and graphics mode
  400.   for display of status report }
  401. begin
  402.   DriveStr := GetDriverName;
  403.   ModeStr := GetModeName(GetGraphMode);
  404. end; { GetDriverAndMode }
  405.  
  406. procedure ReportStatus;
  407. { Display the status of all query functions after InitGraph }
  408. const
  409.   X = 10;
  410. var
  411.   ViewInfo   : ViewPortType;     { Parameters for inquiry procedures }
  412.   LineInfo   : LineSettingsType;
  413.   FillInfo   : FillSettingsType;
  414.   TextInfo   : TextSettingsType;
  415.   Palette    : PaletteType;
  416.   DriverStr  : string;           { Driver and mode strings }
  417.   ModeStr    : string;
  418.   Y          : word;
  419.  
  420. procedure WriteOut(S : string);
  421. { Write out a string and increment to next line }
  422. begin
  423.   OutTextXY(X, Y, S);
  424.   Inc(Y, TextHeight('M')+2);
  425. end; { WriteOut }
  426.  
  427. begin { ReportStatus }
  428.   GetDriverAndMode(DriverStr, ModeStr);   { Get current settings }
  429.   GetViewSettings(ViewInfo);
  430.   GetLineSettings(LineInfo);
  431.   GetFillSettings(FillInfo);
  432.   GetTextSettings(TextInfo);
  433.   GetPalette(Palette);
  434.  
  435.   Y := 4;
  436.   MainWindow('Status report after InitGraph');
  437.   SetTextJustify(LeftText, TopText);
  438.   WriteOut('Graphics device    : '+DriverStr);
  439.   WriteOut('Graphics mode      : '+ModeStr);
  440.   WriteOut('Screen resolution  : (0, 0, '+Int2Str(GetMaxX)+', '+Int2Str(GetMaxY)+')');
  441.   with ViewInfo do
  442.   begin
  443.     WriteOut('Current view port  : ('+Int2Str(x1)+', '+Int2Str(y1)+', '+Int2Str(x2)+', '+Int2Str(y2)+')');
  444.     if ClipOn then
  445.       WriteOut('Clipping           : ON')
  446.     else
  447.       WriteOut('Clipping           : OFF');
  448.   end;
  449.   WriteOut('Current position   : ('+Int2Str(GetX)+', '+Int2Str(GetY)+')');
  450.   WriteOut('Palette entries    : '+Int2Str(Palette.Size));
  451.   WriteOut('GetMaxColor        : '+Int2Str(GetMaxColor));
  452.   WriteOut('Current color      : '+Int2Str(GetColor));
  453.   with LineInfo do
  454.   begin
  455.     WriteOut('Line style         : '+LineStyles[LineStyle]);
  456.     WriteOut('Line thickness     : '+Int2Str(Thickness));
  457.   end;
  458.   with FillInfo do
  459.   begin
  460.     WriteOut('Current fill style : '+FillStyles[Pattern]);
  461.     WriteOut('Current fill color : '+Int2Str(Color));
  462.   end;
  463.   with TextInfo do
  464.   begin
  465.     WriteOut('Current font       : '+Fonts[Font]);
  466.     WriteOut('Text direction     : '+TextDirect[Direction]);
  467.     WriteOut('Character size     : '+Int2Str(CharSize));
  468.     WriteOut('Horizontal justify : '+HorizJust[Horiz]);
  469.     WriteOut('Vertical justify   : '+VertJust[Vert]);
  470.   end;
  471.   WaitToGo;
  472. end; { ReportStatus }
  473.  
  474. procedure FillEllipsePlay;
  475. { Random filled ellipse demonstration }
  476. const
  477.   MaxFillStyles = 12; { patterns 0..11 }
  478. var
  479.   MaxRadius : word;
  480.   FillColor : integer;
  481. begin
  482.   MainWindow('FillEllipse demonstration');
  483.   StatusLine('Esc aborts or press a key');
  484.   MaxRadius := MaxY div 10;
  485.   SetLineStyle(SolidLn, 0, NormWidth);
  486.   repeat
  487.     FillColor := RandColor;
  488.     SetColor(RealDrawColor(FillColor));
  489.     SetFillStyle(Random(MaxFillStyles), RealFillColor(FillColor));
  490.     FillEllipse(Random(MaxX), Random(MaxY),
  491.                 Random(MaxRadius), Random(MaxRadius));
  492.   until KeyPressed;
  493.   WaitToGo;
  494. end; { FillEllipsePlay }
  495.  
  496. procedure SectorPlay;
  497. { Draw random sectors on the screen }
  498. const
  499.   MaxFillStyles = 12; { patterns 0..11 }
  500. var
  501.   MaxRadius : word;
  502.   FillColor : integer;
  503.   EndAngle  : integer;
  504. begin
  505.   MainWindow('Sector demonstration');
  506.   StatusLine('Esc aborts or press a key');
  507.   MaxRadius := MaxY div 10;
  508.   SetLineStyle(SolidLn, 0, NormWidth);
  509.   repeat
  510.     FillColor := RandColor;
  511.     SetColor(RealDrawColor(FillColor));
  512.     SetFillStyle(Random(MaxFillStyles), RealFillColor(FillColor));
  513.     EndAngle := Random(360);
  514.     Sector(Random(MaxX), Random(MaxY), Random(EndAngle), EndAngle,
  515.            Random(MaxRadius), Random(MaxRadius));
  516.   until KeyPressed;
  517.   WaitToGo;
  518. end; { SectorPlay }
  519.  
  520. procedure WriteModePlay;
  521. { Demonstrate the SetWriteMode procedure for XOR lines }
  522. const
  523.   DelayValue = 50;  { milliseconds to delay }
  524. var
  525.   ViewInfo      : ViewPortType;
  526.   Color         : word;
  527.   Left, Top     : integer;
  528.   Right, Bottom : integer;
  529.   Step          : integer; { step for rectangle shrinking }
  530. begin
  531.   MainWindow('SetWriteMode demonstration');
  532.   StatusLine('Esc aborts or press a key');
  533.   GetViewSettings(ViewInfo);
  534.   Left := 0;
  535.   Top := 0;
  536.   with ViewInfo do
  537.   begin
  538.     Right := x2-x1;
  539.     Bottom := y2-y1;
  540.   end;
  541.   Step := Bottom div 50;
  542.   SetColor(RealDrawColor(WhitePixel));
  543.   Line(Left, Top, Right, Bottom);
  544.   Line(Left, Bottom, Right, Top);
  545.   SetWriteMode(XORPut);                    { Set XOR write mode }
  546.   repeat
  547.     Line(Left, Top, Right, Bottom);        { Draw XOR lines }
  548.     Line(Left, Bottom, Right, Top);
  549.     Rectangle(Left, Top, Right, Bottom);   { Draw XOR rectangle }
  550.     Delay(DelayValue);                     { Wait }
  551.     Line(Left, Top, Right, Bottom);        { Erase lines }
  552.     Line(Left, Bottom, Right, Top);
  553.     Rectangle(Left, Top, Right, Bottom);   { Erase rectangle }
  554.     if (Left+Step < Right) and (Top+Step < Bottom) then
  555.       begin
  556.         Inc(Left, Step);                  { Shrink rectangle }
  557.         Inc(Top, Step);
  558.         Dec(Right, Step);
  559.         Dec(Bottom, Step);
  560.       end
  561.     else
  562.       begin
  563.         Color := RandColor;                { New color }
  564.         SetColor(RealDrawColor(Color));
  565.         Left := 0;                         { Original large rectangle }
  566.         Top := 0;
  567.         with ViewInfo do
  568.         begin
  569.           Right := x2-x1;
  570.           Bottom := y2-y1;
  571.         end;
  572.       end;
  573.   until KeyPressed;
  574.   SetWriteMode(CopyPut);                   { back to overwrite mode }
  575.   WaitToGo;
  576. end; { WriteModePlay }
  577.  
  578. procedure AspectRatioPlay;
  579. { Demonstrate  SetAspectRatio command }
  580. var
  581.   ViewInfo   : ViewPortType;
  582.   CenterX    : integer;
  583.   CenterY    : integer;
  584.   Radius     : word;
  585.   Xasp, Yasp : word;
  586.   i          : integer;
  587.   RadiusStep : word;
  588. begin
  589.   MainWindow('SetAspectRatio demonstration');
  590.   GetViewSettings(ViewInfo);
  591.   with ViewInfo do
  592.   begin
  593.     CenterX := (x2-x1) div 2;
  594.     CenterY := (y2-y1) div 2;
  595.     Radius := 3*((y2-y1) div 5);
  596.   end;
  597.   RadiusStep := (Radius div 30);
  598.   Circle(CenterX, CenterY, Radius);
  599.   GetAspectRatio(Xasp, Yasp);
  600.   for i := 1 to 30 do
  601.   begin
  602.     SetAspectRatio(Xasp, Yasp+(I*GetMaxX));    { Increase Y aspect factor }
  603.     Circle(CenterX, CenterY, Radius);
  604.     Dec(Radius, RadiusStep);                   { Shrink radius }
  605.   end;
  606.   Inc(Radius, RadiusStep*30);
  607.   for i := 1 to 30 do
  608.   begin
  609.     SetAspectRatio(Xasp+(I*GetMaxX), Yasp);    { Increase X aspect factor }
  610.     if Radius > RadiusStep then
  611.       Dec(Radius, RadiusStep);                 { Shrink radius }
  612.     Circle(CenterX, CenterY, Radius);
  613.   end;
  614.   SetAspectRatio(Xasp, Yasp);                  { back to original aspect }
  615.   WaitToGo;
  616. end; { AspectRatioPlay }
  617.  
  618. procedure TextPlay;
  619. { Demonstrate text justifications and text sizing }
  620. var
  621.   Size : word;
  622.   W, H, X, Y : word;
  623.   ViewInfo : ViewPortType;
  624. begin
  625.   MainWindow('SetTextJustify / SetUserCharSize demo');
  626.   GetViewSettings(ViewInfo);
  627.   with ViewInfo do
  628.   begin
  629.     SetTextStyle(TriplexFont, VertDir, 4);
  630.     Y := (y2-y1) - 2;
  631.     SetTextJustify(CenterText, BottomText);
  632.     OutTextXY(2*TextWidth('M'), Y, 'Vertical');
  633.     SetTextStyle(TriplexFont, HorizDir, 4);
  634.     SetTextJustify(LeftText, TopText);
  635.     OutTextXY(2*TextWidth('M'), 2, 'Horizontal');
  636.     SetTextJustify(CenterText, CenterText);
  637.     X := (x2-x1) div 2;
  638.     Y := TextHeight('H');
  639.     for Size := 1 to 4 do
  640.     begin
  641.       SetTextStyle(TriplexFont, HorizDir, Size);
  642.       H := TextHeight('M');
  643.       W := TextWidth('M');
  644.       Inc(Y, H);
  645.       OutTextXY(X, Y, 'Size '+Int2Str(Size));
  646.     end;
  647.     Inc(Y, H div 2);
  648.     SetTextJustify(CenterText, TopText);
  649.     SetUserCharSize(5, 6, 3, 2);
  650.     SetTextStyle(TriplexFont, HorizDir, UserCharSize);
  651.     OutTextXY((x2-x1) div 2, Y, 'User defined size!');
  652.   end;
  653.   WaitToGo;
  654. end; { TextPlay }
  655.  
  656. procedure TextDump;
  657. { Dump the complete character sets to the screen }
  658. const
  659.   CGASizes  : array[0..4] of word = (1, 3, 7, 3, 3);
  660.   NormSizes : array[0..4] of word = (1, 4, 7, 4, 4);
  661. var
  662.   Font : word;
  663.   ViewInfo : ViewPortType;
  664.   Ch : char;
  665. begin
  666.   for Font := 0 to 4 do
  667.   begin
  668.     MainWindow(Fonts[Font]+' character set');
  669.     GetViewSettings(ViewInfo);
  670.     with ViewInfo do
  671.     begin
  672.       SetTextJustify(LeftText, TopText);
  673.       MoveTo(2, 3);
  674.       if Font = DefaultFont then
  675.         begin
  676.           SetTextStyle(Font, HorizDir, 1);
  677.           Ch := #0;
  678.           repeat
  679.             OutText(Ch);
  680.             if (GetX + TextWidth('M')) > (x2-x1) then
  681.               MoveTo(2, GetY + TextHeight('M')+3);
  682.             Ch := Succ(Ch);
  683.           until (Ch >= #255);
  684.         end
  685.       else
  686.         begin
  687.           if MaxY < 200 then
  688.             SetTextStyle(Font, HorizDir, CGASizes[Font])
  689.           else
  690.             SetTextStyle(Font, HorizDir, NormSizes[Font]);
  691.           Ch := '!';
  692.           repeat
  693.             OutText(Ch);
  694.             if (GetX + TextWidth('M')) > (x2-x1) then
  695.               MoveTo(2, GetY + TextHeight('M')+3);
  696.             Ch := Succ(Ch);
  697.           until (Ord(Ch) = Ord('~')+1);
  698.         end;
  699.     end; { with }
  700.     WaitToGo;
  701.   end; { for loop }
  702. end; { TextDump }
  703.  
  704. procedure LineToPlay;
  705. { Demonstrate MoveTo and LineTo commands }
  706. const
  707.   MaxPoints = 15;
  708. var
  709.   Points     : array[0..MaxPoints] of PointType;
  710.   ViewInfo   : ViewPortType;
  711.   I, J       : integer;
  712.   CenterX    : integer;   { The center point of the circle }
  713.   CenterY    : integer;
  714.   Radius     : word;
  715.   StepAngle  : word;
  716.   Xasp, Yasp : word;
  717.   Radians    : real;
  718.  
  719. function AdjAsp(Value : integer) : integer;
  720. { Adjust a value for the aspect ratio of the device }
  721. begin
  722.   AdjAsp := (LongInt(Value) * Xasp) div Yasp;
  723. end; { AdjAsp }
  724.  
  725. begin
  726.   MainWindow('MoveTo, LineTo demonstration');
  727.   GetAspectRatio(Xasp, Yasp);
  728.   GetViewSettings(ViewInfo);
  729.   with ViewInfo do
  730.   begin
  731.     CenterX := (x2-x1) div 2;
  732.     CenterY := (y2-y1) div 2;
  733.     Radius := CenterY;
  734.     while (CenterY+AdjAsp(Radius)) < (y2-y1)-20 do
  735.       Inc(Radius);
  736.   end;
  737.   StepAngle := 360 div MaxPoints;
  738.   for I := 0 to MaxPoints - 1 do
  739.   begin
  740.     Radians := (StepAngle * I) * Pi / 180;
  741.     Points[I].X := CenterX + round(Cos(Radians) * Radius);
  742.     Points[I].Y := CenterY - AdjAsp(round(Sin(Radians) * Radius));
  743.   end;
  744.   Circle(CenterX, CenterY, Radius);
  745.   for I := 0 to MaxPoints - 1 do
  746.   begin
  747.     for J := I to MaxPoints - 1 do
  748.     begin
  749.       MoveTo(Points[I].X, Points[I].Y);
  750.       LineTo(Points[J].X, Points[J].Y);
  751.     end;
  752.   end;
  753.   WaitToGo;
  754. end; { LineToPlay }
  755.  
  756. procedure LineRelPlay;
  757. { Demonstrate MoveRel and LineRel commands }
  758. const
  759.   MaxPoints = 12;
  760. var
  761.   Poly     : array[1..MaxPoints] of PointType; { Stores a polygon for filling }
  762.   CurrPort : ViewPortType;
  763.  
  764. procedure DrawTesseract;
  765. { Draw a Tesseract on the screen with relative move and
  766.   line drawing commands, also create a polygon for filling }
  767. const
  768.   CheckerBoard : FillPatternType = (0, $10, $28, $44, $28, $10, 0, 0);
  769. var
  770.   X, Y, W, H   : integer;
  771.  
  772. begin
  773.   GetViewSettings(CurrPort);
  774.   with CurrPort do
  775.   begin
  776.     W := (x2-x1) div 9;
  777.     H := (y2-y1) div 8;
  778.     X := ((x2-x1) div 2) - round(2.5 * W);
  779.     Y := ((y2-y1) div 2) - (3 * H);
  780.  
  781.     { Border around viewport is outer part of polygon }
  782.     Poly[1].X := 0;     Poly[1].Y := 0;
  783.     Poly[2].X := x2-x1; Poly[2].Y := 0;
  784.     Poly[3].X := x2-x1; Poly[3].Y := y2-y1;
  785.     Poly[4].X := 0;     Poly[4].Y := y2-y1;
  786.     Poly[5].X := 0;     Poly[5].Y := 0;
  787.     MoveTo(X, Y);
  788.  
  789.     { Grab the whole in the polygon as we draw }
  790.     MoveRel(0, H);      Poly[6].X := GetX;  Poly[6].Y := GetY;
  791.     MoveRel(W, -H);     Poly[7].X := GetX;  Poly[7].Y := GetY;
  792.     MoveRel(4*W, 0);    Poly[8].X := GetX;  Poly[8].Y := GetY;
  793.     MoveRel(0, 5*H);    Poly[9].X := GetX;  Poly[9].Y := GetY;
  794.     MoveRel(-W, H);     Poly[10].X := GetX; Poly[10].Y := GetY;
  795.     MoveRel(-4*W, 0);   Poly[11].X := GetX; Poly[11].Y := GetY;
  796.     MoveRel(0, -5*H);   Poly[12].X := GetX; Poly[12].Y := GetY;
  797.  
  798.     { Fill the polygon with a user defined fill pattern }
  799.     SetFillPattern(CheckerBoard, RealFillColor(GreenPixel));
  800.     FillPoly(12, Poly);
  801.  
  802.     MoveRel(W, -H);
  803.     LineRel(0, 5*H);   LineRel(2*W, 0);    LineRel(0, -3*H);
  804.     LineRel(W, -H);    LineRel(0, 5*H);    MoveRel(0, -5*H);
  805.     LineRel(-2*W, 0);  LineRel(0, 3*H);    LineRel(-W, H);
  806.     MoveRel(W, -H);    LineRel(W, 0);      MoveRel(0, -2*H);
  807.     LineRel(-W, 0);
  808.  
  809.     { Flood fill the center }
  810.     FloodFill((x2-x1) div 2, (y2-y1) div 2,RealColor(WhitePixel));
  811.   end;
  812. end; { DrawTesseract }
  813.  
  814. begin
  815.   MainWindow('LineRel / MoveRel demonstration');
  816.   GetViewSettings(CurrPort);
  817.   with CurrPort do
  818.     { Move the viewport out 1 pixel from each end }
  819.     SetViewPort(x1-1, y1-1, x2+1, y2+1, ClipOn);
  820.   DrawTesseract;
  821.   WaitToGo;
  822. end; { LineRelPlay }
  823.  
  824. procedure PiePlay;
  825. { Demonstrate  PieSlice and GetAspectRatio commands }
  826. var
  827.   ViewInfo   : ViewPortType;
  828.   CenterX    : integer;
  829.   CenterY    : integer;
  830.   Radius     : word;
  831.   Xasp, Yasp : word;
  832.   X, Y       : integer;
  833.  
  834. function AdjAsp(Value : integer) : integer;
  835. { Adjust a value for the aspect ratio of the device }
  836. begin
  837.   AdjAsp := (LongInt(Value) * Xasp) div Yasp;
  838. end; { AdjAsp }
  839.  
  840. procedure GetTextCoords(AngleInDegrees, Radius : word; var X, Y : integer);
  841. { Get the coordinates of text for pie slice labels }
  842. var
  843.   Radians : real;
  844. begin
  845.   Radians := AngleInDegrees * Pi / 180;
  846.   X := round(Cos(Radians) * Radius);
  847.   Y := round(Sin(Radians) * Radius);
  848. end; { GetTextCoords }
  849.  
  850. begin
  851.   MainWindow('PieSlice / GetAspectRatio demonstration');
  852.   GetAspectRatio(Xasp, Yasp);
  853.   GetViewSettings(ViewInfo);
  854.   with ViewInfo do
  855.   begin
  856.     CenterX := (x2-x1) div 2;
  857.     CenterY := ((y2-y1) div 2) + 20;
  858.     Radius := (y2-y1) div 3;
  859.     while AdjAsp(Radius) < round((y2-y1) / 3.6) do
  860.       Inc(Radius);
  861.   end;
  862.   SetTextStyle(TriplexFont, HorizDir, 4);
  863.   SetTextJustify(CenterText, TopText);
  864.   OutTextXY(CenterX, 0, 'This is a pie chart!');
  865.  
  866.   SetTextStyle(TriplexFont, HorizDir, 3);
  867.  
  868.   SetFillStyle(SolidFill, RealFillColor(RandColor));
  869.   PieSlice(CenterX+10, CenterY-AdjAsp(10), 0, 90, Radius);
  870.   GetTextCoords(45, Radius, X, Y);
  871.   SetTextJustify(LeftText, BottomText);
  872.   OutTextXY(CenterX+10+X+TextWidth('H'), CenterY-AdjAsp(10+Y), '25 %');
  873.  
  874.   SetFillStyle(HatchFill, RealFillColor(RandColor));
  875.   PieSlice(CenterX, CenterY, 225, 360, Radius);
  876.   GetTextCoords(293, Radius, X, Y);
  877.   SetTextJustify(LeftText, TopText);
  878.   OutTextXY(CenterX+X+TextWidth('H'), CenterY-AdjAsp(Y), '37.5 %');
  879.  
  880.   SetFillStyle(InterleaveFill, RealFillColor(RandColor));
  881.   PieSlice(CenterX-10, CenterY, 135, 225, Radius);
  882.   GetTextCoords(180, Radius, X, Y);
  883.   SetTextJustify(RightText, CenterText);
  884.   OutTextXY(CenterX-10+X-TextWidth('H'), CenterY-AdjAsp(Y), '25 %');
  885.  
  886.   SetFillStyle(WideDotFill, RealFillColor(RandColor));
  887.   PieSlice(CenterX, CenterY, 90, 135, Radius);
  888.   GetTextCoords(112, Radius, X, Y);
  889.   SetTextJustify(RightText, BottomText);
  890.   OutTextXY(CenterX+X-TextWidth('H'), CenterY-AdjAsp(Y), '12.5 %');
  891.  
  892.   WaitToGo;
  893. end; { PiePlay }
  894.  
  895. procedure Bar3DPlay;
  896. { Demonstrate Bar3D command }
  897. const
  898.   NumBars   = 7;  { The number of bars drawn }
  899.   BarHeight : array[1..NumBars] of byte = (1, 3, 2, 5, 4, 2, 1);
  900.   YTicks    = 5;  { The number of tick marks on the Y axis }
  901. var
  902.   ViewInfo : ViewPortType;
  903.   H        : word;
  904.   XStep    : real;
  905.   YStep    : real;
  906.   I, J     : integer;
  907.   Depth    : word;
  908.   Color    : word;
  909. begin
  910.   MainWindow('Bar3D / Rectangle demonstration');
  911.   H := 3*TextHeight('M');
  912.   GetViewSettings(ViewInfo);
  913.   SetTextJustify(CenterText, TopText);
  914.   SetTextStyle(TriplexFont, HorizDir, 4);
  915.   OutTextXY(MaxX div 2, 6, 'These are 3D bars !');
  916.   SetTextStyle(DefaultFont, HorizDir, 1);
  917.   with ViewInfo do
  918.     SetViewPort(x1+50, y1+40, x2-50, y2-10, ClipOn);
  919.   GetViewSettings(ViewInfo);
  920.   with ViewInfo do
  921.   begin
  922.     Line(H, H, H, (y2-y1)-H);
  923.     Line(H, (y2-y1)-H, (x2-x1)-H, (y2-y1)-H);
  924.     YStep := ((y2-y1)-(2*H)) / YTicks;
  925.     XStep := ((x2-x1)-(2*H)) / NumBars;
  926.     J := (y2-y1)-H;
  927.     SetTextJustify(CenterText, CenterText);
  928.  
  929.     { Draw the Y axis and ticks marks }
  930.     for I := 0 to Yticks do
  931.     begin
  932.       Line(H div 2, J, H, J);
  933.       OutTextXY(0, J, Int2Str(I));
  934.       J := Round(J-Ystep);
  935.     end;
  936.  
  937.  
  938.     Depth := trunc(0.25 * XStep);    { Calculate depth of bar }
  939.  
  940.     { Draw X axis, bars, and tick marks }
  941.     SetTextJustify(CenterText, TopText);
  942.     J := H;
  943.     for I := 1 to Succ(NumBars) do
  944.     begin
  945.       SetColor(RealDrawColor(WhitePixel));
  946.       Line(J, (y2-y1)-H, J, (y2-y1-3)-(H div 2));
  947.       OutTextXY(J, (y2-y1)-(H div 2), Int2Str(I-1));
  948.       if I <> Succ(NumBars) then
  949.       begin
  950.         Color := RandColor;
  951.         SetFillStyle(I, RealFillColor(Color));
  952.         SetColor(RealDrawColor(Color));
  953.         Bar3D(J, round((y2-y1-H)-(BarHeight[I] * Ystep)),
  954.                  round(J+Xstep-Depth), round((y2-y1)-H-1), Depth, TopOn);
  955.         J := Round(J+Xstep);
  956.       end;
  957.     end;
  958.  
  959.   end;
  960.   WaitToGo;
  961. end; { Bar3DPlay }
  962.  
  963. procedure SolidBarPlay;
  964. { Draw random solid bars on the screen }
  965. var
  966.   MaxWidth  : integer;
  967.   MaxHeight : integer;
  968.   ViewInfo  : ViewPortType;
  969.   Color     : word;
  970. begin
  971.   MainWindow('Random Solid Bars');
  972.   StatusLine('Esc aborts or press a key');
  973.   GetViewSettings(ViewInfo);
  974.   with ViewInfo do
  975.   begin
  976.     MaxWidth := x2-x1;
  977.     MaxHeight := y2-y1;
  978.   end;
  979.   repeat
  980.     Color := Random(GetMaxColor);  { RandColor }
  981.     SetColor(RealDrawColor(Color));
  982.     SetFillStyle(SolidFill, RealFillColor(Color));
  983.     Bar3D(Random(MaxWidth), Random(MaxHeight),
  984.           Random(MaxWidth), Random(MaxHeight), 0, TopOff);
  985.   until KeyPressed;
  986.   WaitToGo;
  987. end; { SolidBarPlay }
  988.  
  989. procedure BarPlay;
  990. { Demonstrate Bar command }
  991. const
  992.   NumBars   = 5;
  993.   BarHeight : array[1..NumBars] of byte = (1, 3, 5, 2, 4);
  994.   Styles    : array[1..NumBars] of byte = (1, 3, 10, 5, 9);
  995. var
  996.   ViewInfo  : ViewPortType;
  997.   BarNum    : word;
  998.   H         : word;
  999.   XStep     : real;
  1000.   YStep     : real;
  1001.   I, J      : integer;
  1002.   Color     : word;
  1003. begin
  1004.   MainWindow('Bar / Rectangle demonstration');
  1005.   H := 3*TextHeight('M');
  1006.   GetViewSettings(ViewInfo);
  1007.   SetTextJustify(CenterText, TopText);
  1008.   SetTextStyle(TriplexFont, HorizDir, 4);
  1009.   OutTextXY(MaxX div 2, 6, 'These are 2D bars !');
  1010.   SetTextStyle(DefaultFont, HorizDir, 1);
  1011.   with ViewInfo do
  1012.     SetViewPort(x1+50, y1+30, x2-50, y2-10, ClipOn);
  1013.   GetViewSettings(ViewInfo);
  1014.   with ViewInfo do
  1015.   begin
  1016.     Line(H, H, H, (y2-y1)-H);
  1017.     Line(H, (y2-y1)-H, (x2-x1)-H, (y2-y1)-H);
  1018.     YStep := ((y2-y1)-(2*H)) / NumBars;
  1019.     XStep := ((x2-x1)-(2*H)) / NumBars;
  1020.     J := (y2-y1)-H;
  1021.     SetTextJustify(CenterText, CenterText);
  1022.  
  1023.     { Draw Y axis with tick marks }
  1024.     for I := 0 to NumBars do
  1025.     begin
  1026.       Line(H div 2, J, H, J);
  1027.       OutTextXY(0, J, Int2Str(i));
  1028.       J := Round(J-Ystep);
  1029.     end;
  1030.  
  1031.     { Draw X axis, bars, and tick marks }
  1032.     J := H;
  1033.     SetTextJustify(CenterText, TopText);
  1034.     for I := 1 to Succ(NumBars) do
  1035.     begin
  1036.       SetColor(RealDrawColor(WhitePixel));
  1037.       Line(J, (y2-y1)-H, J, (y2-y1-3)-(H div 2));
  1038.       OutTextXY(J, (y2-y1)-(H div 2), Int2Str(I));
  1039.       if I <> Succ(NumBars) then
  1040.       begin
  1041.         Color := RandColor;
  1042.         SetFillStyle(Styles[I], RealFillColor(Color));
  1043.         SetColor(RealDrawColor(Color));
  1044.         Bar(J, round((y2-y1-H)-(BarHeight[I] * Ystep)), round(J+Xstep), (y2-y1)-H-1);
  1045.         Rectangle(J, round((y2-y1-H)-(BarHeight[I] * Ystep)), round(J+Xstep), (y2-y1)-H-1);
  1046.       end;
  1047.       J := Round(J+Xstep);
  1048.     end;
  1049.  
  1050.   end;
  1051.   WaitToGo;
  1052. end; { BarPlay }
  1053.  
  1054. procedure CirclePlay;
  1055. { Draw random circles on the screen }
  1056. var
  1057.   MaxRadius : word;
  1058. begin
  1059.   MainWindow('Circle demonstration');
  1060.   StatusLine('Esc aborts or press a key');
  1061.   MaxRadius := MaxY div 10;
  1062.   SetLineStyle(SolidLn, 0, NormWidth);
  1063.   repeat
  1064.     SetColor(RealDrawColor(RandColor));
  1065.     Circle(Random(MaxX), Random(MaxY), Random(MaxRadius));
  1066.   until KeyPressed;
  1067.   WaitToGo;
  1068. end; { CirclePlay }
  1069.  
  1070.  
  1071. procedure RandBarPlay;
  1072. { Draw random bars on the screen }
  1073. var
  1074.   MaxWidth  : integer;
  1075.   MaxHeight : integer;
  1076.   ViewInfo  : ViewPortType;
  1077.   Color     : word;
  1078. begin
  1079.   MainWindow('Random Bars');
  1080.   StatusLine('Esc aborts or press a key');
  1081.   GetViewSettings(ViewInfo);
  1082.   with ViewInfo do
  1083.   begin
  1084.     MaxWidth := x2-x1;
  1085.     MaxHeight := y2-y1;
  1086.   end;
  1087.   repeat
  1088.     Color := RandColor;
  1089.     SetColor(RealDrawColor(Color));
  1090.     SetFillStyle(Random(CloseDotFill)+1, RealFillColor(Color));
  1091.     Bar3D(Random(MaxWidth), Random(MaxHeight),
  1092.           Random(MaxWidth), Random(MaxHeight), 0, TopOff);
  1093.   until KeyPressed;
  1094.   WaitToGo;
  1095. end; { RandBarPlay }
  1096.  
  1097. procedure ArcPlay;
  1098. { Draw random arcs on the screen }
  1099. var
  1100.   MaxRadius : word;
  1101.   EndAngle : word;
  1102.   ArcInfo : ArcCoordsType;
  1103. begin
  1104.   MainWindow('Arc / GetArcCoords demonstration');
  1105.   StatusLine('Esc aborts or press a key');
  1106.   MaxRadius := MaxY div 10;
  1107.   repeat
  1108.     SetColor(RealDrawColor(RandColor));
  1109.     EndAngle := Random(360);
  1110.     SetLineStyle(SolidLn, 0, NormWidth);
  1111.     Arc(Random(MaxX), Random(MaxY), Random(EndAngle), EndAngle, Random(MaxRadius));
  1112.     GetArcCoords(ArcInfo);
  1113.     with ArcInfo do
  1114.     begin
  1115.       Line(X, Y, XStart, YStart);
  1116.       Line(X, Y, Xend, Yend);
  1117.     end;
  1118.   until KeyPressed;
  1119.   WaitToGo;
  1120. end; { ArcPlay }
  1121.  
  1122. procedure PutPixelPlay;
  1123. { Demonstrate the PutPixel and GetPixel commands }
  1124. const
  1125.   Seed   = 1962; { A seed for the random number generator }
  1126.   NumPts = 2000; { The number of pixels plotted }
  1127.   Esc    = #27;
  1128. var
  1129.   I : word;
  1130.   X, Y, Color : word;
  1131.   XMax, YMax  : integer;
  1132.   ViewInfo    : ViewPortType;
  1133. begin
  1134.   MainWindow('PutPixel / GetPixel demonstration');
  1135.   StatusLine('Esc aborts or press a key...');
  1136.  
  1137.   GetViewSettings(ViewInfo);
  1138.   with ViewInfo do
  1139.   begin
  1140.     XMax := (x2-x1-1);
  1141.     YMax := (y2-y1-1);
  1142.   end;
  1143.  
  1144.   while not KeyPressed do
  1145.   begin
  1146.     { Plot random pixels }
  1147.     RandSeed := Seed;
  1148.     I := 0;
  1149.     while (not KeyPressed) and (I < NumPts) do
  1150.     begin
  1151.       Inc(I);
  1152.       PutPixel(Random(XMax)+1, Random(YMax)+1, RealColor(RandColor));
  1153.     end;
  1154.  
  1155.     { Erase pixels }
  1156.     RandSeed := Seed;
  1157.     I := 0;
  1158.     while (not KeyPressed) and (I < NumPts) do
  1159.     begin
  1160.       Inc(I);
  1161.       X := Random(XMax)+1;
  1162.       Y := Random(YMax)+1;
  1163.       Color := GetPixel(X,Y);
  1164.       inline($89/$56/<Color);  (* Used to load 15-bit color value *)
  1165.       if Color = RandColor then
  1166.         PutPixel(X, Y, RealColor(0));
  1167.     end;
  1168.   end;
  1169.   WaitToGo;
  1170. end; { PutPixelPlay }
  1171.  
  1172. procedure PutImagePlay;
  1173. { Demonstrate the GetImage and PutImage commands }
  1174.  
  1175. const
  1176.   r  = 20;
  1177.   StartX = 100;
  1178.   StartY = 150;
  1179.  
  1180. var
  1181.   CurPort : ViewPortType;
  1182.  
  1183. procedure MoveSaucer(var X, Y : integer; Width, Height : integer);
  1184. var
  1185.   Step : integer;
  1186. begin
  1187.   Step := Random(2*r);
  1188.   if Odd(Step) then
  1189.     Step := -Step;
  1190.   X := X + Step;
  1191.   Step := Random(r);
  1192.   if Odd(Step) then
  1193.     Step := -Step;
  1194.   Y := Y + Step;
  1195.  
  1196.   { Make saucer bounce off viewport walls }
  1197.   with CurPort do
  1198.   begin
  1199.     if (x1 + X + Width - 1 > x2) then
  1200.       X := x2-x1 - Width + 1
  1201.     else
  1202.       if (X < 0) then
  1203.         X := 0;
  1204.     if (y1 + Y + Height - 1 > y2) then
  1205.       Y := y2-y1 - Height + 1
  1206.     else
  1207.       if (Y < 0) then
  1208.         Y := 0;
  1209.   end;
  1210. end; { MoveSaucer }
  1211.  
  1212. var
  1213.   Pausetime : word;
  1214.   Saucer    : pointer;
  1215.   X, Y      : integer;
  1216.   ulx, uly  : word;
  1217.   lrx, lry  : word;
  1218.   Size      : word;
  1219.   I         : word;
  1220. begin
  1221.   ClearDevice;
  1222.   FullPort;
  1223.  
  1224.   { PaintScreen }
  1225.   ClearDevice;
  1226.   MainWindow('GetImage / PutImage Demonstration');
  1227.   StatusLine('Esc aborts or press a key...');
  1228.   GetViewSettings(CurPort);
  1229.  
  1230.   { DrawSaucer }
  1231.   Ellipse(StartX, StartY, 0, 360, r, (r div 3)+2);
  1232.   Ellipse(StartX, StartY-4, 190, 357, r, r div 3);
  1233.   Line(StartX+7, StartY-6, StartX+10, StartY-12);
  1234.   Circle(StartX+10, StartY-12, 2);
  1235.   Line(StartX-7, StartY-6, StartX-10, StartY-12);
  1236.   Circle(StartX-10, StartY-12, 2);
  1237.   SetFillStyle(SolidFill, RealFillColor(BluePixel));
  1238.   FloodFill(StartX+1, StartY+4, RealColor(WhitePixel));
  1239.  
  1240.   { ReadSaucerImage }
  1241.   ulx := StartX-(r+1);
  1242.   uly := StartY-14;
  1243.   lrx := StartX+(r+1);
  1244.   lry := StartY+(r div 3)+3;
  1245.  
  1246.   Size := ImageSize(ulx, uly, lrx, lry);
  1247.   GetMem(Saucer, Size);
  1248.   GetImage(ulx, uly, lrx, lry, Saucer^);
  1249.   PutImage(ulx, uly, Saucer^, XORput);               { erase image }
  1250.   { Plot some "stars" }
  1251.   for I := 1 to 1000 do
  1252.     PutPixel(Random(MaxX), Random(MaxY), RealColor(RandColor));
  1253.   X := MaxX div 2;
  1254.   Y := MaxY div 2;
  1255.   PauseTime := 70;
  1256.  
  1257.   { Move the saucer around }
  1258.   repeat
  1259.     X := (X div 8)*8;
  1260.     PutImage(X, Y, Saucer^, XORput);                 { draw image }
  1261.     Delay(PauseTime);
  1262.     PutImage(X, Y, Saucer^, XORput);                 { erase image }
  1263.     MoveSaucer(X, Y, lrx - ulx + 1, lry - uly + 1);  { width/height }
  1264.   until KeyPressed;
  1265.   FreeMem(Saucer, size);
  1266.   WaitToGo;
  1267. end; { PutImagePlay }
  1268.  
  1269. procedure PolyPlay;
  1270. { Draw random polygons with random fill styles on the screen }
  1271. const
  1272.   MaxPts = 5;
  1273. type
  1274.   PolygonType = array[1..MaxPts] of PointType;
  1275. var
  1276.   Poly : PolygonType;
  1277.   I, Color : word;
  1278. begin
  1279.   MainWindow('FillPoly demonstration');
  1280.   StatusLine('Esc aborts or press a key...');
  1281.   repeat
  1282.     Color := RandColor;
  1283.     SetFillStyle(Random(11)+1, RealFillColor(Color));
  1284.     SetColor(RealDrawColor(Color));
  1285.     for I := 1 to MaxPts do
  1286.       with Poly[I] do
  1287.       begin
  1288.         X := Random(MaxX);
  1289.         Y := Random(MaxY);
  1290.       end;
  1291.     FillPoly(MaxPts, Poly);
  1292.   until KeyPressed;
  1293.   WaitToGo;
  1294. end; { PolyPlay }
  1295.  
  1296. procedure FillStylePlay;
  1297. { Display all of the predefined fill styles available }
  1298. var
  1299.   Style    : word;
  1300.   Width    : word;
  1301.   Height   : word;
  1302.   X, Y     : word;
  1303.   I, J     : word;
  1304.   ViewInfo : ViewPortType;
  1305.  
  1306. procedure DrawBox(X, Y : word);
  1307. begin
  1308.   SetFillStyle(Style, RealFillColor(WhitePixel));
  1309.   with ViewInfo do
  1310.     Bar(X, Y, X+Width, Y+Height);
  1311.   Rectangle(X, Y, X+Width, Y+Height);
  1312.   OutTextXY(X+(Width div 2), Y+Height+4, Int2Str(Style));
  1313.   Inc(Style);
  1314. end; { DrawBox }
  1315.  
  1316. begin
  1317.   MainWindow('Pre-defined fill styles');
  1318.   GetViewSettings(ViewInfo);
  1319.   with ViewInfo do
  1320.   begin
  1321.     Width := 2 * ((x2+1) div 13);
  1322.     Height := 2 * ((y2-10) div 10);
  1323.   end;
  1324.   X := Width div 2;
  1325.   Y := Height div 2;
  1326.   Style := 0;
  1327.   for J := 1 to 3 do
  1328.   begin
  1329.     for I := 1 to 4 do
  1330.     begin
  1331.       DrawBox(X, Y);
  1332.       Inc(X, (Width div 2) * 3);
  1333.     end;
  1334.     X := Width div 2;
  1335.     Inc(Y, (Height div 2) * 3);
  1336.   end;
  1337.   SetTextJustify(LeftText, TopText);
  1338.   WaitToGo;
  1339. end; { FillStylePlay }
  1340.  
  1341. procedure FillPatternPlay;
  1342. { Display some user defined fill patterns }
  1343. const
  1344.   Patterns : array[0..11] of FillPatternType = (
  1345.   ($AA, $55, $AA, $55, $AA, $55, $AA, $55),
  1346.   ($33, $33, $CC, $CC, $33, $33, $CC, $CC),
  1347.   ($F0, $F0, $F0, $F0, $F, $F, $F, $F),
  1348.   (0, $10, $28, $44, $28, $10, 0, 0),
  1349.   (0, $70, $20, $27, $25, $27, $4, $4),
  1350.   (0, 0, 0, $18, $18, 0, 0, 0),
  1351.   (0, 0, $3C, $3C, $3C, $3C, 0, 0),
  1352.   (0, $7E, $7E, $7E, $7E, $7E, $7E, 0),
  1353.   (0, 0, $22, $8, 0, $22, $1C, 0),
  1354.   ($FF, $7E, $3C, $18, $18, $3C, $7E, $FF),
  1355.   (0, $10, $10, $7C, $10, $10, 0, 0),
  1356.   (0, $42, $24, $18, $18, $24, $42, 0));
  1357. var
  1358.   Style    : word;
  1359.   Width    : word;
  1360.   Height   : word;
  1361.   X, Y     : word;
  1362.   I, J     : word;
  1363.   ViewInfo : ViewPortType;
  1364.  
  1365. procedure DrawBox(X, Y : word);
  1366. begin
  1367.   SetFillPattern(Patterns[Style], RealFillColor(WhitePixel));
  1368.   with ViewInfo do
  1369.     Bar(X, Y, X+Width, Y+Height);
  1370.   Rectangle(X, Y, X+Width, Y+Height);
  1371.   Inc(Style);
  1372. end; { DrawBox }
  1373.  
  1374. begin
  1375.   MainWindow('User defined fill styles');
  1376.   GetViewSettings(ViewInfo);
  1377.   with ViewInfo do
  1378.   begin
  1379.     Width := 2 * ((x2+1) div 13);
  1380.     Height := 2 * ((y2-10) div 10);
  1381.   end;
  1382.   X := Width div 2;
  1383.   Y := Height div 2;
  1384.   Style := 0;
  1385.   for J := 1 to 3 do
  1386.   begin
  1387.     for I := 1 to 4 do
  1388.     begin
  1389.       DrawBox(X, Y);
  1390.       Inc(X, (Width div 2) * 3);
  1391.     end;
  1392.     X := Width div 2;
  1393.     Inc(Y, (Height div 2) * 3);
  1394.   end;
  1395.   SetTextJustify(LeftText, TopText);
  1396.   WaitToGo;
  1397. end; { FillPatternPlay }
  1398.  
  1399. procedure ColorPlay;
  1400. { Display all of the colors available for the current driver and mode }
  1401. var
  1402.   Color    : word;
  1403.   Width    : word;
  1404.   Height   : word;
  1405.   X, Y     : word;
  1406.   I, J     : word;
  1407.   ViewInfo : ViewPortType;
  1408.  
  1409. procedure DrawBox(X, Y : word);
  1410. begin
  1411.   SetFillStyle(SolidFill, RealFillColor(Color));
  1412.   SetColor(RealDrawColor(Color));
  1413.   with ViewInfo do
  1414.     Bar(X, Y, X+Width, Y+Height);
  1415.   Rectangle(X, Y, X+Width, Y+Height);
  1416.   if Color = 0 then
  1417.   begin
  1418.     SetColor(RealDrawColor(WhitePixel));
  1419.     Rectangle(X, Y, X+Width, Y+Height);
  1420.   end;
  1421.   Color := Succ(Color);
  1422. end; { DrawBox }
  1423.  
  1424. begin
  1425.   begin
  1426.     MainWindow('256 Color demonstration');
  1427.     Color := $0;
  1428.     GetViewSettings(ViewInfo);
  1429.     with ViewInfo do
  1430.     begin
  1431.       Width := 2*((x2-x1+1) div 46);
  1432.       Height := 2*((y2-x1+1) div 47);
  1433.     end;
  1434.     X := Width div 3;
  1435.     Y := Height div 3;
  1436.     for J := 1 to 16 do
  1437.     begin
  1438.       for I := 1 to 16 do
  1439.       begin
  1440.         DrawBox(X, Y);
  1441.         Inc(X,(Width div 2)*3);
  1442.       end;
  1443.       X := Width div 3;
  1444.       Inc(Y,(Height div 2)*3);
  1445.     end;
  1446.   end;
  1447.   WaitToGo;
  1448. end; { ColorPlay }
  1449.  
  1450. procedure PalettePlay;
  1451. { Demonstrate the use of the SetRGBPalette command }
  1452. const
  1453.   XBars = 15;
  1454.   YBars = 10;
  1455. type
  1456.   RGBColor   = record
  1457.                  R, G, B : byte;
  1458.                end;
  1459.   VGAPalette = array[0..255] of RGBColor;
  1460.  
  1461. var
  1462.   I, J     : word;
  1463.   X, Y     : word;
  1464.   Color    : word;
  1465.   ViewInfo : ViewPortType;
  1466.   Width    : word;
  1467.   Height   : word;
  1468.   VGAPal   : VGAPalette;
  1469.   Rand     : integer;
  1470.  
  1471. procedure ReadDACBlock(Start, Count : integer; var Pal : VGAPalette);
  1472. var
  1473.   Regs : Registers;
  1474. begin
  1475.   with Regs do
  1476.   begin
  1477.     AH := $10;
  1478.     AL := $17;
  1479.     BX := Start;
  1480.     CX := Count;
  1481.     ES := Seg(Pal);
  1482.     DX := Ofs(Pal);
  1483.   end;
  1484.   Intr($10, Regs);
  1485. end;
  1486.  
  1487. procedure SetDACBlock(Start, Count : integer; var Pal : VGAPalette);
  1488. var
  1489.   Regs : Registers;
  1490. begin
  1491.   with Regs do
  1492.   begin
  1493.     AH := $10;
  1494.     AL := $12;
  1495.     BX := Start;
  1496.     CX := Count;
  1497.     ES := Seg(Pal);
  1498.     DX := Ofs(Pal);
  1499.   end;
  1500.   Intr($10, Regs);
  1501. end;
  1502.  
  1503. begin
  1504.   ReadDACBlock(0, 256, VGAPal);
  1505.   MainWindow('SetRGBPalette demonstration');
  1506.   StatusLine('Press any key...');
  1507.   GetViewSettings(ViewInfo);
  1508.   with ViewInfo do
  1509.   begin
  1510.     Width := (x2-x1) div XBars;
  1511.     Height := (y2-y1) div YBars;
  1512.   end;
  1513.   X := 0; Y := 0;
  1514.   Color := 0;
  1515.   for J := 1 to YBars do
  1516.   begin
  1517.     for I := 1 to XBars do
  1518.     begin
  1519.       SetFillStyle(SolidFill, RealFillColor(Color));
  1520.       Bar(X, Y, X+Width, Y+Height);
  1521.       Inc(X, Width+1);
  1522.       Inc(Color);
  1523.       Color := Color mod 16;
  1524.     end;
  1525.     X := 0;
  1526.     Inc(Y, Height+1);
  1527.   end;
  1528.   repeat
  1529.     {SetPalette(Random(16), VGAPal[Random(256)]);}
  1530.     with VGAPal[Random(16)] do
  1531.       SetRGBPalette(Random(16), R, G, B);
  1532.   until KeyPressed;
  1533.   SetDACBlock(0, 256, VGAPal);
  1534.   WaitToGo;
  1535. end; { PalettePlay }
  1536.  
  1537. procedure CrtModePlay;
  1538. { Demonstrate the use of RestoreCrtMode and SetGraphMode }
  1539. var
  1540.   ViewInfo : ViewPortType;
  1541.   Ch       : char;
  1542. begin
  1543.   MainWindow('SetGraphMode / RestoreCrtMode demo');
  1544.   GetViewSettings(ViewInfo);
  1545.   SetTextJustify(CenterText, CenterText);
  1546.   with ViewInfo do
  1547.   begin
  1548.     OutTextXY((x2-x1) div 2, (y2-y1) div 2, 'Now you are in graphics mode');
  1549.     StatusLine('Press any key for text mode...');
  1550.     repeat until KeyPressed;
  1551.     Ch := ReadKey;
  1552.     RestoreCrtmode;
  1553.     Writeln('Now you are in text mode.');
  1554.     Write('Press any key to go back to graphics...');
  1555.     repeat until KeyPressed;
  1556.     Ch := ReadKey;
  1557.     SetGraphMode(GetGraphMode);
  1558.     MainWindow('SetGraphMode / RestoreCrtMode demo');
  1559.     SetTextJustify(CenterText, CenterText);
  1560.     OutTextXY((x2-x1) div 2, (y2-y1) div 2, 'Back in graphics mode...');
  1561.   end;
  1562.   WaitToGo;
  1563. end; { CrtModePlay }
  1564.  
  1565. procedure LineStylePlay;
  1566. { Demonstrate the predefined line styles available }
  1567. var
  1568.   Style    : word;
  1569.   Step     : word;
  1570.   X, Y     : word;
  1571.   ViewInfo : ViewPortType;
  1572.  
  1573. begin
  1574.   ClearDevice;
  1575.   DefaultColors;
  1576.   MainWindow('Pre-defined line styles');
  1577.   GetViewSettings(ViewInfo);
  1578.   with ViewInfo do
  1579.   begin
  1580.     X := 35;
  1581.     Y := 10;
  1582.     Step := (x2-x1) div 11;
  1583.     SetTextJustify(LeftText, TopText);
  1584.     OutTextXY(X, Y, 'NormWidth');
  1585.     SetTextJustify(CenterText, TopText);
  1586.     for Style := 0 to 3 do
  1587.     begin
  1588.       SetLineStyle(Style, 0, NormWidth);
  1589.       Line(X, Y+20, X, Y2-40);
  1590.       OutTextXY(X, Y2-30, Int2Str(Style));
  1591.       Inc(X, Step);
  1592.     end;
  1593.     Inc(X, 2*Step);
  1594.     SetTextJustify(LeftText, TopText);
  1595.     OutTextXY(X, Y, 'ThickWidth');
  1596.     SetTextJustify(CenterText, TopText);
  1597.     for Style := 0 to 3 do
  1598.     begin
  1599.       SetLineStyle(Style, 0, ThickWidth);
  1600.       Line(X, Y+20, X, Y2-40);
  1601.       OutTextXY(X, Y2-30, Int2Str(Style));
  1602.       Inc(X, Step);
  1603.     end;
  1604.   end;
  1605.   SetTextJustify(LeftText, TopText);
  1606.   WaitToGo;
  1607. end; { LineStylePlay }
  1608.  
  1609. procedure UserLineStylePlay;
  1610. { Demonstrate user defined line styles }
  1611. var
  1612.   Style    : word;
  1613.   X, Y, I  : word;
  1614.   ViewInfo : ViewPortType;
  1615. begin
  1616.   MainWindow('User defined line styles');
  1617.   GetViewSettings(ViewInfo);
  1618.   with ViewInfo do
  1619.   begin
  1620.     X := 4;
  1621.     Y := 10;
  1622.     Style := 0;
  1623.     I := 0;
  1624.     while X < X2-4 do
  1625.     begin
  1626.       {$B+}
  1627.       Style := Style or (1 shl (I mod 16));
  1628.       {$B-}
  1629.       SetLineStyle(UserBitLn, Style, NormWidth);
  1630.       Line(X, Y, X, (y2-y1)-Y);
  1631.       Inc(X, 5);
  1632.       Inc(I);
  1633.       if Style = 65535 then
  1634.       begin
  1635.         I := 0;
  1636.         Style := 0;
  1637.       end;
  1638.     end;
  1639.   end;
  1640.   WaitToGo;
  1641. end; { UserLineStylePlay }
  1642.  
  1643.  
  1644. procedure SayGoodbye;
  1645. { Say goodbye and then exit the program }
  1646. var
  1647.   ViewInfo : ViewPortType;
  1648. begin
  1649.   MainWindow('');
  1650.   GetViewSettings(ViewInfo);
  1651.   SetTextStyle(TriplexFont, HorizDir, 4);
  1652.   SetTextJustify(CenterText, CenterText);
  1653.   with ViewInfo do
  1654.     OutTextXY((x2-x1) div 2, (y2-y1) div 2, 'That''s all folks!');
  1655.   StatusLine('Press any key to quit...');
  1656.   repeat until KeyPressed;
  1657. end; { SayGoodbye }
  1658.  
  1659. begin { program body }
  1660.   ClrScr;
  1661.   writeln('VGA BGI Demo Program  Copyright(c) 1987,1989 Borland International, Inc.');
  1662.   writeln;
  1663.   Initialize;
  1664.   ReportStatus;
  1665.   AspectRatioPlay;
  1666.   FillEllipsePlay;
  1667.   SectorPlay;
  1668.   WriteModePlay;
  1669.   ColorPlay;
  1670.   PalettePlay;
  1671.   PutPixelPlay;
  1672.   PutImagePlay;
  1673.   RandBarPlay;
  1674.   SolidBarPlay;
  1675.   BarPlay;
  1676.   Bar3DPlay;
  1677.   ArcPlay;
  1678.   CirclePlay;
  1679.   PiePlay;
  1680.   LineToPlay;
  1681.   LineRelPlay;
  1682.   LineStylePlay;
  1683.   UserLineStylePlay;
  1684.   TextDump;
  1685.   TextPlay;
  1686.   CrtModePlay;
  1687.   FillStylePlay;
  1688.   FillPatternPlay;
  1689.   PolyPlay;
  1690.   SayGoodbye;
  1691.   CloseGraph;
  1692. end.
  1693.